package com.yzf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yzf.common.CustomException;
import com.yzf.dto.DishDto;
import com.yzf.dto.SetmealDto;
import com.yzf.entity.Dish;
import com.yzf.entity.DishFlavor;
import com.yzf.entity.Setmeal;
import com.yzf.entity.SetmealDish;
import com.yzf.mapper.SetmealMapper;
import com.yzf.service.SetmealDishService;
import com.yzf.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yzf
 * @description
 * @since 2023/2/16 16:26
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        this.save(setmealDto);
        Long id = setmealDto.getId();
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item)->{
            item.setSetmealId(id);
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public SetmealDto GetWithId(Long id) {
        SetmealDto setmealDto = new SetmealDto();
        List<SetmealDish> setmealDishes =setmealDto.getSetmealDishes();
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(id!=null,SetmealDish::getSetmealId,id);
        setmealDishes=setmealDishService.list(queryWrapper);
        setmealDto.setSetmealDishes(setmealDishes);
        Setmeal byId = this.getById(id);
        BeanUtils.copyProperties(byId,setmealDto);
        return setmealDto;
    }

    @Override
    public void updateWithDish(SetmealDto setmealDto) {
        this.updateById(setmealDto);
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getDishId,setmealDto.getId());
        setmealDishService.remove(queryWrapper);
        List<SetmealDish> dishes = setmealDto.getSetmealDishes();
        Long id = setmealDto.getId();
        dishes.stream().map((item)->{
            item.setSetmealId(id);
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(dishes);
    }

    @Transactional
    @Override
    public void deleteByListId(List<Long> ids) {

            LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.in(Setmeal::getId,ids);
            queryWrapper.eq(Setmeal::getStatus,1);
            int count = this.count(queryWrapper);
            if(count>0){
                throw new CustomException("当前套餐正在售卖中,不能删除");
            }
                this.removeByIds(ids);
                LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                lambdaQueryWrapper.in(SetmealDish::getSetmealId, ids);
                setmealDishService.remove(lambdaQueryWrapper);
        }



}
